library(cregg)
library(ggplot2)
library(ggpubr)
rm(list=ls())

#### Load data 

load("data.RData")

#### Formulas for models

f1 <- vote_choice ~ age + prof+career + gender + legeff + voteror + voterknow + smedia ## Formula
f2 <- better_pol ~ age + prof+career + gender + legeff + voteror + voterknow + smedia ## Formula
f3 <- prob_vote ~ age + prof+career + gender + legeff + voteror + voterknow + smedia ## Formula

#### Create function run models for each country

countries <- unique(data$country)
estimates_countries <- list()
estimates_mm_countries <- list()

for (i in countries) {
  
  data_country <- data[data$country==i,]
  data_country$id <- as.numeric(as.factor(data_country$id))
  
  
  acmes1 <- cj(data_country, f1, id = ~id)
  acmes2 <- cj(data[data$country==i,], f2, id = ~id)
  acmes3 <- cj(data[data$country==i,], f3, id = ~id)
  
  estimates_countries[[which(countries == i)]] <- list(esimate_vote_choice = acmes1,
                                                       esimate_better_pol = acmes2,
                                                       esimate_prob_vote = acmes3,
                                                       country = i)
  
  mm1 <- mm(data[data$country==i,], f1, id = ~id)
  mm2 <- mm(data[data$country==i,], f2, id = ~id)
  mm3 <- mm(data[data$country==i,], f3, id = ~id)
  
  estimates_mm_countries[[which(countries == i)]] <- list(esimate_vote_choice = mm1,
                                                          esimate_better_pol = mm2,
                                                          esimate_prob_vote = mm3,
                                                          country = i)
  
  print(i)
}


### Create function to exctract and format ACMEs by country 

create_data_country <- function(i) {
  data_compare_countries <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                              "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                              "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                              "Gender", "Man", "Woman",
                                                              "Legislative Effectiveness", "Most projects accepted", "Some projects accepted",
                                                              "Presence in Constituency", "Little time with voters in consituency", "Lot of time with voters in consituency",
                                                              "Knowledge of voters", "Low knowledge of voters' positions", "Good knowledge of voters' position",
                                                              "Social Media", "Regularly communicate views", "Do not communicate views"),3),
                                               labels_score = rep(c(13:30, 1:12), 3),
                                               estimate = c(NA, estimates_countries[[i]][["esimate_vote_choice"]][["estimate"]][1:5],
                                                            NA, estimates_countries[[i]][["esimate_vote_choice"]][["estimate"]][6:10],
                                                            NA, estimates_countries[[i]][["esimate_vote_choice"]][["estimate"]][11:12],
                                                            NA, estimates_countries[[i]][["esimate_vote_choice"]][["estimate"]][13:14],
                                                            NA, estimates_countries[[i]][["esimate_vote_choice"]][["estimate"]][15:16],
                                                            NA, estimates_countries[[i]][["esimate_vote_choice"]][["estimate"]][17:18],
                                                            NA, estimates_countries[[i]][["esimate_vote_choice"]][["estimate"]][19:20],
                                                            NA, estimates_countries[[i]][["esimate_vote_choice"]][["estimate"]][21:22],
                                                            
                                                            NA, estimates_countries[[i]][["esimate_better_pol"]][["estimate"]][1:5],
                                                            NA, estimates_countries[[i]][["esimate_better_pol"]][["estimate"]][6:10],
                                                            NA, estimates_countries[[i]][["esimate_better_pol"]][["estimate"]][11:12],
                                                            NA, estimates_countries[[i]][["esimate_better_pol"]][["estimate"]][13:14],
                                                            NA, estimates_countries[[i]][["esimate_better_pol"]][["estimate"]][15:16],
                                                            NA, estimates_countries[[i]][["esimate_better_pol"]][["estimate"]][17:18],
                                                            NA, estimates_countries[[i]][["esimate_better_pol"]][["estimate"]][19:20],
                                                            NA, estimates_countries[[i]][["esimate_better_pol"]][["estimate"]][21:22],
                                                            
                                                            NA, estimates_countries[[i]][["esimate_prob_vote"]][["estimate"]][1:5],
                                                            NA, estimates_countries[[i]][["esimate_prob_vote"]][["estimate"]][6:10],
                                                            NA, estimates_countries[[i]][["esimate_prob_vote"]][["estimate"]][11:12],
                                                            NA, estimates_countries[[i]][["esimate_prob_vote"]][["estimate"]][13:14],
                                                            NA, estimates_countries[[i]][["esimate_prob_vote"]][["estimate"]][15:16],
                                                            NA, estimates_countries[[i]][["esimate_prob_vote"]][["estimate"]][17:18],
                                                            NA, estimates_countries[[i]][["esimate_prob_vote"]][["estimate"]][19:20],
                                                            NA, estimates_countries[[i]][["esimate_prob_vote"]][["estimate"]][21:22]),
                                               
                                               lower = c(NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["lower"]][2:5],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["lower"]][7:10],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["lower"]][12],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["lower"]][14],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["lower"]][16],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["lower"]][18],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["lower"]][20],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["lower"]][22],
                                                         
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["lower"]][2:5],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["lower"]][7:10],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["lower"]][12],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["lower"]][14],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["lower"]][16],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["lower"]][18],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["lower"]][20],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["lower"]][22],
                                                         
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["lower"]][2:5],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["lower"]][7:10],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["lower"]][12],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["lower"]][14],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["lower"]][16],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["lower"]][18],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["lower"]][20],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["lower"]][22]),
                                               upper = c(NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["upper"]][2:5],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["upper"]][7:10],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["upper"]][12],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["upper"]][14],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["upper"]][16],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["upper"]][18],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["upper"]][20],
                                                         NA, NA, estimates_countries[[i]][["esimate_vote_choice"]][["upper"]][22],
                                                         
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["upper"]][2:5],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["upper"]][7:10],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["upper"]][12],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["upper"]][14],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["upper"]][16],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["upper"]][18],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["upper"]][20],
                                                         NA, NA, estimates_countries[[i]][["esimate_better_pol"]][["upper"]][22],
                                                         
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["upper"]][2:5],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["upper"]][7:10],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["upper"]][12],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["upper"]][14],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["upper"]][16],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["upper"]][18],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["upper"]][20],
                                                         NA, NA, estimates_countries[[i]][["esimate_prob_vote"]][["upper"]][22]),
                                               country = rep(estimates_countries[[i]][["country"]], 90),
                                               country_score = rep(i, 90),
                                               outcome = c(rep("Vote Choice", 30), rep("Better Politician", 30), rep("Probability to vote", 30))))
  
  
  
  data_compare_countries$labels <- factor(data_compare_countries$labels, levels = c("Legislative Effectiveness", "Most projects accepted", "Some projects accepted",
                                                                                    "Presence in Constituency", "Little time with voters in consituency", "Lot of time with voters in consituency",
                                                                                    "Knowledge of voters", "Low knowledge of voters' positions", "Good knowledge of voters' position",
                                                                                    "Social Media", "Regularly communicate views", "Do not communicate views",
                                                                                    "Age", "33", "43", "53", "63", "73",
                                                                                    "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                                                    "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                                                    "Gender", "Man", "Woman"))
  
  return(data_compare_countries)
  
}

### Create function to exctract and format marginal means by country 

create_data_country_mm <- function(i) {
  data_compare_countries_mm <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                                 "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                                 "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                                 "Gender", "Man", "Woman",
                                                                 "Legislative Effectiveness", "Most projects accepted", "Some projects accepted",
                                                                 "Presence in Constituency", "Little time with voters in consituency", "Lot of time with voters in consituency",
                                                                 "Knowledge of voters", "Low knowledge of voters' positions", "Good knowledge of voters' position",
                                                                 "Social Media", "Regularly communicate views", "Do not communicate views"),3),
                                                  labels_score = rep(c(13:30, 1:12), 3),
                                                  estimate = c(NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["estimate"]][1:5],
                                                               NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["estimate"]][6:10],
                                                               NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["estimate"]][11:12],
                                                               NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["estimate"]][13:14],
                                                               NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["estimate"]][15:16],
                                                               NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["estimate"]][17:18],
                                                               NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["estimate"]][19:20],
                                                               NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["estimate"]][21:22],
                                                               
                                                               NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["estimate"]][1:5],
                                                               NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["estimate"]][6:10],
                                                               NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["estimate"]][11:12],
                                                               NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["estimate"]][13:14],
                                                               NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["estimate"]][15:16],
                                                               NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["estimate"]][17:18],
                                                               NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["estimate"]][19:20],
                                                               NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["estimate"]][21:22],
                                                               
                                                               NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["estimate"]][1:5],
                                                               NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["estimate"]][6:10],
                                                               NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["estimate"]][11:12],
                                                               NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["estimate"]][13:14],
                                                               NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["estimate"]][15:16],
                                                               NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["estimate"]][17:18],
                                                               NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["estimate"]][19:20],
                                                               NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["estimate"]][21:22]),
                                                  
                                                  lower = c(NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["lower"]][1:5],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["lower"]][6:10],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["lower"]][11:12],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["lower"]][13:14],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["lower"]][15:16],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["lower"]][17:18],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["lower"]][19:20],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["lower"]][21:22],
                                                            
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["lower"]][1:5],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["lower"]][6:10],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["lower"]][11:12],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["lower"]][13:14],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["lower"]][15:16],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["lower"]][17:18],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["lower"]][19:20],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["lower"]][21:22],
                                                            
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["lower"]][1:5],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["lower"]][6:10],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["lower"]][11:12],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["lower"]][13:14],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["lower"]][15:16],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["lower"]][17:18],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["lower"]][19:20],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["lower"]][21:22]),
                                                  upper = c(NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["upper"]][1:5],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["upper"]][6:10],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["upper"]][11:12],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["upper"]][13:14],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["upper"]][15:16],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["upper"]][17:18],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["upper"]][19:20],
                                                            NA, estimates_mm_countries[[i]][["esimate_vote_choice"]][["upper"]][21:22],
                                                            
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["upper"]][1:5],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["upper"]][6:10],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["upper"]][11:12],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["upper"]][13:14],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["upper"]][15:16],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["upper"]][17:18],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["upper"]][19:20],
                                                            NA, estimates_mm_countries[[i]][["esimate_better_pol"]][["upper"]][21:22],
                                                            
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["upper"]][1:5],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["upper"]][6:10],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["upper"]][11:12],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["upper"]][13:14],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["upper"]][15:16],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["upper"]][17:18],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["upper"]][19:20],
                                                            NA, estimates_mm_countries[[i]][["esimate_prob_vote"]][["upper"]][21:22]),
                                                  country = rep(estimates_mm_countries[[i]][["country"]], 90),
                                                  country_score = rep(i, 90),
                                                  outcome = c(rep("Vote Choice", 30), rep("Better Politician", 30), rep("Probability to vote", 30))))
  
  
  
  data_compare_countries_mm$labels <- factor(data_compare_countries_mm$labels, levels = c("Legislative Effectiveness", "Most projects accepted", "Some projects accepted",
                                                                                          "Presence in Constituency", "Little time with voters in consituency", "Lot of time with voters in consituency",
                                                                                          "Knowledge of voters", "Low knowledge of voters' positions", "Good knowledge of voters' position",
                                                                                          "Social Media", "Regularly communicate views", "Do not communicate views",
                                                                                          "Age", "33", "43", "53", "63", "73",
                                                                                          "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                                                          "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                                                          "Gender", "Man", "Woman"))
  
  return(data_compare_countries_mm)
  
}


### Run models and extract data

for (i in 1:7) {
  if(i==1) {
    
    data_countries <- create_data_country(i)
    data_countries_mm <- create_data_country_mm(i)
    
  } else {
    dta_countries <- create_data_country(i)
    
    data_countries <- rbind(data_countries, dta_countries)
    
    dta_countries_mm <- create_data_country_mm(i)
    
    data_countries_mm <- rbind(data_countries_mm, dta_countries_mm)
  }
}



### Adapt the labels to plot the results
data_countries <- data_countries %>% 
  mutate(country_labels_num1 = as.numeric(paste0(labels_score, ".", country_score)),
         country_labels_num2 = ifelse(country_labels_num1<2, 1, country_labels_num1),
         type = "Acmes")

data_countries_mm <- data_countries_mm %>% 
  mutate(country_labels_num1 = as.numeric(paste0(labels_score, ".", country_score)),
         country_labels_num2 = ifelse(country_labels_num1<2, 1, country_labels_num1),
         type = "Marginal Means")


### Figure A6
png("Figures/Figure A6.png", width = 2500, height = 3000, res=300)
ggplot(rbind(data_countries[data_countries$outcome=="Vote Choice" & data_countries$country!="Switzerland" & data_countries$country!="Germany",], 
             data_countries_mm[data_countries_mm$outcome=="Vote Choice" & data_countries_mm$country!="Switzerland" & data_countries_mm$country!="Germany",]))+
  geom_point(aes(x=estimate, y=country_labels_num2, color = country))+
  geom_errorbarh(aes(xmin = lower, xmax=upper, color = country, y=country_labels_num2))+
  ylim(c(1,30))+
  scale_y_continuous(breaks = c(1:30), labels = c("Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                  "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                  "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                  "Social Media", "Communicates regularly", "Does not communicate often","Age", "33", "43", "53", "63", "73",
                                                  "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                  "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                  "Gender", "Man", "Woman"),
                     trans = "reverse")+
  
  scale_color_manual(values = c("red", "blue", "green", "dark green", "grey", "orange", "purple"))+
  geom_vline(aes(xintercept = c(rep(0, 150), rep(.5, 150))), linewidth=.1)+
  facet_wrap(~type,
             labeller = labeller(
               type = c(`Marginal Means` = "Marginal Means", `Acmes` = "Average Marginal Component Effect")
             ), scales = 'free_x')+
  ylab("")+
  theme_minimal()+
  theme(legend.position = "bottom", legend.title = element_blank(), 
        panel.border = element_rect(colour = "black", fill=NA, size=.5),
        axis.text.y = element_text(face = c("bold", rep("plain", 2), 
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2))))
dev.off() 


#### Figure A7
png("Figures/Figure A7.png", width = 2500, height = 3000, res=300)
ggplot(rbind(data_countries[data_countries$outcome=="Probability to vote" & data_countries$country!="Switzerland" & data_countries$country!="Germany",], 
             data_countries_mm[data_countries_mm$outcome=="Probability to vote" & data_countries_mm$country!="Switzerland" & data_countries_mm$country!="Germany",]))+
  geom_point(aes(x=estimate, y=country_labels_num2, color = country))+
  geom_errorbarh(aes(xmin = lower, xmax=upper, color = country, y=country_labels_num2))+
  scale_y_continuous(breaks = c(1:30), labels = c("Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                  "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                  "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                  "Social Media", "Communicates regularly", "Does not communicate often","Age", "33", "43", "53", "63", "73",
                                                  "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                  "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                  "Gender", "Man", "Woman"),
                     trans = "reverse")+
  scale_color_manual(values = c("red", "blue", "green", "dark green", "grey", "orange", "purple"))+
  geom_vline(aes(xintercept = c(rep(0, 150), rep(5, 150))), linewidth=.1)+
  facet_wrap(~type,
             labeller = labeller(
               type = c(`Marginal Means` = "Marginal Means", `Acmes` = "Average Marginal Component Effect")
             ), scales = 'free_x')+
  ylab("")+
  theme_minimal()+
  theme(legend.position = "bottom", legend.title = element_blank(), 
        panel.border = element_rect(colour = "black", fill=NA, size=.5),
        axis.text.y = element_text(face = c("bold", rep("plain", 2), 
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2))))
dev.off() 

#### Figure A8
png("Figures/Figure A8.png", width = 2500, height = 3000, res=300)
ggplot(rbind(data_countries[data_countries$outcome=="Better Politician" & data_countries$country!="Switzerland" & data_countries$country!="Germany",], 
             data_countries_mm[data_countries_mm$outcome=="Better Politician" & data_countries_mm$country!="Switzerland" & data_countries_mm$country!="Germany",]))+
  geom_point(aes(x=estimate, y=country_labels_num2, color = country))+
  geom_errorbarh(aes(xmin = lower, xmax=upper, color = country, y=country_labels_num2))+
  scale_y_continuous(breaks = c(1:30), labels = c("Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                  "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                  "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                  "Social Media", "Communicates regularly", "Does not communicate often","Age", "33", "43", "53", "63", "73",
                                                  "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                  "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                  "Gender", "Man", "Woman"),
                     trans = "reverse")+
  scale_color_manual(values = c("red", "blue", "green", "dark green", "grey", "orange", "purple"))+
  geom_vline(aes(xintercept = c(rep(0, 150), rep(.5, 150))), linewidth=.1)+
  facet_wrap(~type,
             labeller = labeller(
               type = c(`Marginal Means` = "Marginal Means", `Acmes` = "Average Marginal Component Effect")
             ), scales = 'free_x')+
  ylab("")+
  theme_minimal()+
  theme(legend.position = "bottom", legend.title = element_blank(), 
        panel.border = element_rect(colour = "black", fill=NA, size=.5),
        axis.text.y = element_text(face = c("bold", rep("plain", 2), 
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2))))
dev.off() 


#### Figure A22

png("Figures/Figure A22.png", width = 2500, height = 3000, res=300)
ggplot(rbind(data_countries[data_countries$outcome=="Vote Choice",], 
             data_countries_mm[data_countries_mm$outcome=="Vote Choice",]))+
  geom_point(aes(x=estimate, y=country_labels_num2, color = country))+
  geom_errorbarh(aes(xmin = lower, xmax=upper, color = country, y=country_labels_num2))+
  scale_y_continuous(breaks = c(1:30), labels = c("Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                  "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                  "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                  "Social Media", "Communicates regularly", "Does not communicate often","Age", "33", "43", "53", "63", "73",
                                                  "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                  "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                  "Gender", "Man", "Woman"),
                     trans = "reverse")+
  scale_color_manual(values = c("red", "blue", "green", "dark green", "grey", "orange", "purple"))+
  geom_vline(aes(xintercept = c(rep(0, 210), rep(.5, 210))), linewidth=.1)+
  facet_wrap(~type,
             labeller = labeller(
               type = c(`Marginal Means` = "Marginal Means", `Acmes` = "Average Marginal Component Effect")
             ), scales = 'free_x')+
  ylab("")+
  theme_minimal()+
  theme(legend.position = "bottom", legend.title = element_blank(), 
        panel.border = element_rect(colour = "black", fill=NA, size=.5),
        axis.text.y = element_text(face = c("bold", rep("plain", 2), 
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2))))
dev.off() 



